package czh.cocoa.animation;

import czh.cocoa.foundation.NSObject;
import czh.cocoa.foundation.NSString;

public abstract class CAMediaTimingFunction extends NSObject {

	public final static NSString kCAMediaTimingFunctionLinear = NSTR("linear");
	
	public final static NSString kCAMediaTimingFunctionEaseIn = NSTR("easein");
	
	public final static NSString kCAMediaTimingFunctionEaseOut = NSTR("easeout");
	
	public final static NSString kCAMediaTimingFunctionEaseInEaseOut = NSTR("easeinout");
	
	public final static CAMediaTimingFunction Linear = 
		     CAMediaTimingFunction.functionWithName(kCAMediaTimingFunctionLinear);
	
	public final static CAMediaTimingFunction EaseIn = 
	     CAMediaTimingFunction.functionWithName(kCAMediaTimingFunctionEaseIn);
	
	public final static CAMediaTimingFunction EaseOut = 
	     CAMediaTimingFunction.functionWithName(kCAMediaTimingFunctionEaseOut);
	
	public final static CAMediaTimingFunction EaseInEaseOut = 
	     CAMediaTimingFunction.functionWithName(kCAMediaTimingFunctionEaseInEaseOut);
	
	public static CAMediaTimingFunction functionWithName(NSString name) {
		if (name.isEqual(kCAMediaTimingFunctionLinear)) {
			return new CAMediaTimingFunctionLinear();
		} else if (name.isEqual(kCAMediaTimingFunctionEaseIn)) {
			return new CAMediaTimingFunctionEaseIn();
		} else if (name.isEqual(kCAMediaTimingFunctionEaseOut)) {
			return new CAMediaTimingFunctionEaseOut();
		} else if (name.isEqual(kCAMediaTimingFunctionEaseInEaseOut)) {
			return new CAMediaTimingFunctionEaseInEaseOut();
		}   
		
		return null;
	}
	
	public abstract float getInterpolation(float input);
	
	private static class CAMediaTimingFunctionLinear extends CAMediaTimingFunction {
		@Override
		public float getInterpolation(float input) {
			return input;
		}
	}
	
	private static class CAMediaTimingFunctionEaseIn extends CAMediaTimingFunction {
		@Override
		public float getInterpolation(float input) {
			 return input * input;
		}
	}
	
	private static class CAMediaTimingFunctionEaseOut extends CAMediaTimingFunction {
		@Override
		public float getInterpolation(float input) {
			 return (float)(1.0f - (1.0f - input) * (1.0f - input));
		}
	}
	
	private static class CAMediaTimingFunctionEaseInEaseOut extends CAMediaTimingFunction {
		@Override
		public float getInterpolation(float input) {
			 return (float)(Math.cos((input + 1) * Math.PI) / 2.0f) + 0.5f;
		}
	}
	
}

